//
//  JKView.m
//  CookGuest
//
//  Created by LiuYanJun on 15/9/12.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "JKView.h"

@implementation JKView

-(id)initWithFrame:(CGRect)frame
{
    
    
    [self animation];
    
    self.knowledgeArray = [[NSMutableArray alloc]init];
    
    self.count = 1;
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.knowledgeArray = [[NSMutableArray alloc]init];
        
        self.JKtableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - (SCREEN_HEIGHT / 14) -30) style:UITableViewStylePlain];
        [self addSubview:self.JKtableView];
        self.JKtableView.delegate = self;
        self.JKtableView.dataSource = self;
        
        [self AFN];
        
        self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 4 + 20)];
        self.JKtableView.tableHeaderView = self.bgView;
      
        self.titlePicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.titlePicBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 4.5);
        [self.titlePicBtn addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];
        [self.titlePicBtn addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:self.titlePicBtn];
        
        
        CGFloat xlen = (SCREEN_WIDTH - 55 ) / 3;   //图片宽度
        
        self.leftLable = [[UILabel alloc]initWithFrame:CGRectMake(20, SCREEN_HEIGHT / 4.1 , xlen / 2 , SCREEN_HEIGHT / 29)];
        self.leftLable.text = @"推荐";
        self.leftLable.textAlignment = 1;
        self.leftLable.font = [UIFont systemFontOfSize:14];
        self.leftLable.textColor = [UIColor redColor];
        self.leftLable.layer.borderColor = [[UIColor redColor]CGColor];
        self.leftLable.layer.borderWidth = 1;
        [self.bgView addSubview:self.leftLable];
        
        
        
        
        
        
        self.centerLableBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.centerLableBtn addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];
        self.centerLableBtn.frame = CGRectMake(xlen / 2 + 30, SCREEN_HEIGHT / 4.1, SCREEN_WIDTH / 1.7, SCREEN_HEIGHT / 29);
        [self.centerLableBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.centerLableBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [self.centerLableBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [self.bgView addSubview:self.centerLableBtn];
        
        
        
        
        self.rightLable = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 5 * 4, SCREEN_HEIGHT / 4.1, xlen / 1.5, SCREEN_HEIGHT / 29)];
        self.rightLable.font = [UIFont systemFontOfSize:12];
        self.rightLable.textColor = [UIColor lightGrayColor];
        [self.bgView addSubview:self.rightLable];
        
        
        
        
        [self.JKtableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(AFN)];
        [self.JKtableView reloadData];
    }
    return self;
}

-(void)AFN
{
    
    NSString *str = @"%E5%81%A5%E5%BA%B7%E5%85%BB%E7%94%9F";
    NSString *url = [NSString stringWithFormat:@"http://api.xiangha.com/zhishi5/getNousList?type=classify&name=%@&pinyin=ys&page=%ld",str,self.count];
    
    [AFN urlString:url getAFNdata:^(id responseObject) {
        
        
        
        NSMutableDictionary *dicData = [responseObject objectForKey:@"data"];
        NSMutableArray *nousArray = [dicData objectForKey:@"nous"];
        for (NSMutableDictionary *dic in nousArray) {
            
            KnowledgeModel *knowledgeModel = [[KnowledgeModel alloc]init];
            [knowledgeModel setValuesForKeysWithDictionary:dic];
            
            [self.knowledgeArray addObject:knowledgeModel];
            
            
            
            //    给titlePicBtn赋图片
            
            NSDictionary *dic = [nousArray objectAtIndex:0];
            NSString *img = [dic objectForKey:@"img"];
            [self.titlePicBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:img] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"default"]];
            
            
            
            //    给centerLableBtn赋值
            
            NSString *str = [dic objectForKey:@"title"];
            [self.centerLableBtn setTitle:str forState:UIControlStateNormal];
            
            //    给rightLable赋赋值
            
            NSString *str1 = [dic objectForKey:@"allClick"];
            self.rightLable.text = [NSString stringWithFormat:@"%@浏览",str1];
            
            
            self.codeStr = [dic objectForKey:@"code"];
            
            
        }
        [self.JKtableView reloadData];
        
        [self.JKtableView.footer endRefreshing];
        
        
        [self.animation removeFromSuperview];
    }];
    
    self.count ++;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.knowledgeArray.count - 1;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ADJUSTHEIGHT*190;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId =@"cell";
    KnowledgeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == NO) {
        
        cell = [[KnowledgeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    KnowledgeModel *k = [self.knowledgeArray objectAtIndex:indexPath.row + 1];
    [cell getModel:k];
    
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Knowledge_SecViewController *sec = [[Knowledge_SecViewController alloc]init];
    [self.delegate pushViewController:sec];
    
    
    KnowledgeModel *k = [self.knowledgeArray objectAtIndex:indexPath.row + 1];
    sec.getCode = k.code;
    sec.getContent = k.content;
    sec.getTitle = k.title;
    
}



-(void)Click
{
    Knowledge_SecViewController *sec = [[Knowledge_SecViewController alloc]init];
    [self.delegate pushViewController:sec];
    
    sec.getCode = self.codeStr;
}




#pragma mark - 加载动画
- (void)animationView
{
    self.animation = [[UIView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 49 -64)];
    [self addSubview:self.animation];
    
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 65, SCREEN_HEIGHT / 2 -100, SCREEN_WIDTH / 375 * 130, SCREEN_HEIGHT / 667 *130)];
    
    [imageView setBackgroundColor:[UIColor redColor]];
    for (NSInteger i = 1; i < 5; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld" , i]];
        [arr addObject:image];
    }
    [self.animation addSubview:imageView];
    imageView.animationImages = arr;
    imageView.animationRepeatCount = 0;
    imageView.animationDuration = 3;
    [imageView startAnimating];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, SCREEN_HEIGHT /2-130, SCREEN_WIDTH / 375 * 100, SCREEN_HEIGHT / 667 * 30)];
    label.text = @"玩命加载中...";
    [self.animation addSubview:label];
    
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
